package itsm.isperp.module.controller.task;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.task.TskTask;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.task.TskInspectCiCheckService;
import itsm.isperp.module.service.task.TskInspectTemplateCiCheckService;
import itsm.isperp.module.service.task.TskTaskService;

import java.sql.Timestamp;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("task/task")
public class TskTaskController extends BaseProcessController<TskTask> {

	/**
	 * 
	 */
	@Autowired
	protected TskTaskService tskTaskService;
	@Autowired
	protected ProjProjectService projProjectService;

	@Autowired
	AppDictService appDictService;

	@Override
	public TskTaskService getService() {
		return tskTaskService;
	}

	@Override
	public String getProceessKey() {
		return "taskProcess";
	}

	@Override
	public String getPageTitle() {
		return "计划任务";
	}

	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {

		String projectId = req.getParameter("projectId");
		ModelAndView mv = this.getModelAndView();

		TskTask model = null;
		if (StringUtils.isEmpty(id)) {

			model = new TskTask();
			model.setId(HibernateUUIDGenerator.generate());
			model.setProjectId(projectId);

			IUser user = ContextHolder.getLoginUser();

			model.setPlanPrincipalId(user.getUsername());
			model.setPlanPrincipalName(user.getFullname());

		} else {
			model = this.getService().findOne(id);
		}
		mv.addObject("model", model);

		// 项目阶段
		mv.addObject("projectStages",
				appDictService.findByType("PROJECT_STAGE"));
		// 任务重要程度
		mv.addObject("taskPrioirties",
				appDictService.findByType("TASK_PRIORITY"));
		if (StringUtils.isNotEmpty(projectId)) {

			JqGridRequest request = new JqGridRequest();
			request.setPageable(false);
			request.setSelectColumns("id", "name");
			request.addFilterRule("id", JqGridFilterRuleOp.eq, projectId);

			mv.addObject("projects", projProjectService.findAll(request));
		} else {
			mv.addObject("projects",
					projProjectService.findAllPublished("id", "name"));
		}
		return mv;
	}

	@RequestMapping("handleEdit")
	public ModelAndView handleEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		request.addFilterRule("id", JqGridFilterRuleOp.eq, id);
		Map<String, Object> detail = this.tskTaskService
				.findAllDetailByRequest(request).getContent().get(0);
		mv.addObject("detail", detail);
		TskTask model = this.tskTaskService.findOne(id);

		IUser user = ContextHolder.getLoginUser();

		model.setActPrincipalId(user.getUsername());
		model.setActPrincipalName(user.getFullname());
		model.setActEndTime(new Timestamp(System.currentTimeMillis()));

		mv.addObject("model", model);

		return mv;

	}

	@RequestMapping("acceptEdit")
	public ModelAndView acceptEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		request.addFilterRule("id", JqGridFilterRuleOp.eq, id);
		Map<String, Object> detail = this.tskTaskService
				.findAllDetailByRequest(request).getContent().get(0);
		mv.addObject("detail", detail);
		TskTask model = this.tskTaskService.findOne(id);
		mv.addObject("model", model);
		return mv;
	}
	
	@RequestMapping("closeEdit")
	public ModelAndView closeEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		request.addFilterRule("id", JqGridFilterRuleOp.eq, id);
		Map<String, Object> detail = this.tskTaskService
				.findAllDetailByRequest(request).getContent().get(0);
		mv.addObject("detail", detail);
		TskTask model = this.tskTaskService.findOne(id);
		mv.addObject("model", model);
		return mv;
	}

	@RequestMapping("acceptCiAuditEdit")
	public ModelAndView acceptCiAuditEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		request.addFilterRule("id", JqGridFilterRuleOp.eq, id);
		Map<String, Object> detail = this.tskTaskService
				.findAllDetailByRequest(request).getContent().get(0);
		mv.addObject("detail", detail);
		TskTask model = this.tskTaskService.findOne(id);

		IUser user = ContextHolder.getLoginUser();

		model.setActPrincipalId(user.getUsername());
		model.setActPrincipalName(user.getFullname());
		model.setActEndTime(new Timestamp(System.currentTimeMillis()));

		// 设置更新审计时间为当前时间
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		String sql = "update tsk_inspect_ci_info set task_ciaudit_date=now() where task_id='"
				+ id + "'";
		jdbcTemplate.update(sql);
		mv.addObject("model", model);

		return mv;
	}

	@Override
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = super.manage(req);

		// 任务重要程度
		mv.addObject("taskPrioirties",
				appDictService.findByType("TASK_PRIORITY"));

		// 项目
		DataRequest dataProject = new JqGridRequest();
		dataProject.isPageable(false);
		dataProject.setSelectColumns("id", "name");
		mv.addObject("projectId", projProjectService.findAll(dataProject)
				.getContent());

		return mv;
	}

	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultMessage save(TskTask model, HttpServletRequest req) {

		TskInspectCiCheckService tskInspectCiCheckService = ContextHolder
				.getSpringBean("tskInspectCiCheckService");

		TskInspectTemplateCiCheckService tskInspectTemplateCiCheckService = ContextHolder
				.getSpringBean("tskInspectTemplateCiCheckService");

		this.getService().saveOrUpdate(model, req);

		tskInspectCiCheckService.saveValues(req);

		tskInspectTemplateCiCheckService.saveValues(req);

		return ResultMessage.newInstance(true, null, model.getId());
	}

}
